<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/12/3
 * Time: 下午7:19
 */
class Vin
{
    public static function search($vin_code){
        $url = 'http://pj.am123.com/jydataweb/initBrnadIcon';
        $clxh = Vin::get_clxh($url,$vin_code);
        $vin['fgwName'] = $clxh[0]->fgwName;
        $vin['clxh'] = $clxh[0]->clxh;

        $pl = Vin::get_pl($url,$vin['clxh']);
        $vin['pl'] = $pl[0]->pl;

        $bsq = Vin::get_bsq($url,$vin['clxh'],$vin['pl']);
        $vin['bsq'] = $bsq[0]->bsq;

        $nk = Vin::get_nk($url,$vin['clxh'],$vin['pl'],$vin['bsq']);
        $vin['nk'] = $nk[0]->nk;

        $pzxh = Vin::get_pzxh($url,$vin['clxh'],$vin['pl'],$vin['bsq'],$vin['nk']);
        $models = array();
        foreach($pzxh as $_pzxh){
            $model = array(
                'pacode' => $_pzxh->pacode,
                'pzxh' => $_pzxh->pzxh,
            );
            array_push($models,$model);
        }
        $vin['model'] = $models;

        return $vin;
    }

    public static function get_pzxh($url,$clxh,$pl,$bsq,$nk){
        $request = array(
            'returnType' => 'div',
            'requestType' => 'changeNktoPzxh',
            'pl' => $pl,
            'bsq' => $bsq,
            'nk' => $nk,
            'clxh' => $clxh
        );
        $response = Httpful::post($url)
            ->addHeader('Cookie','JSESSIONID=fvwdWflDSp07rlvLLd85PGJXLvGbzDMDBCRM8VClrZQ6vK0sCGZY!1315059231; Hm_lvt_2c2ab0a70d6928a332084e64a1f3117a=1449108707; Hm_lpvt_2c2ab0a70d6928a332084e64a1f3117a=1449141674')
            ->timeoutIn(15)
            ->body($request)
            ->sendsForm()->send();

        return json_decode($response->body);
    }

    public static function get_nk($url,$clxh,$pl,$bsq){
        $request = array(
            'returnType' => 'div',
            'requestType' => 'changeBsqtoNk',
            'pl' => $pl,
            'bsq' => $bsq,
            'clxh' => $clxh
        );
        $response = Httpful::post($url)
            ->addHeader('Cookie','JSESSIONID=fvwdWflDSp07rlvLLd85PGJXLvGbzDMDBCRM8VClrZQ6vK0sCGZY!1315059231; Hm_lvt_2c2ab0a70d6928a332084e64a1f3117a=1449108707; Hm_lpvt_2c2ab0a70d6928a332084e64a1f3117a=1449141674')
            ->timeoutIn(15)
            ->body($request)
            ->sendsForm()->send();
        return json_decode(mb_convert_encoding($response->body,"UTF8","GBK"));
    }

    public static function get_bsq($url,$clxh,$pl){
        $request = array(
            'returnType' => 'div',
            'requestType' => 'changePltoBsq',
            'pl' => $pl,
            'clxh' => $clxh
        );
        $response = Httpful::post($url)
            ->addHeader('Cookie','JSESSIONID=fvwdWflDSp07rlvLLd85PGJXLvGbzDMDBCRM8VClrZQ6vK0sCGZY!1315059231; Hm_lvt_2c2ab0a70d6928a332084e64a1f3117a=1449108707; Hm_lpvt_2c2ab0a70d6928a332084e64a1f3117a=1449141674')
            ->timeoutIn(15)
            ->body($request)
            ->sendsForm()->send();
        return json_decode(mb_convert_encoding($response->body,"UTF8","GBK"));
    }

    public static function get_pl($url,$clxh){
        $request = array(
            'returnType' => 'div',
            'requestType' => 'changeClxhtoPl',
            'clxh' => $clxh
        );
        $response = Httpful::post($url)
            ->addHeader('Cookie','JSESSIONID=fvwdWflDSp07rlvLLd85PGJXLvGbzDMDBCRM8VClrZQ6vK0sCGZY!1315059231; Hm_lvt_2c2ab0a70d6928a332084e64a1f3117a=1449108707; Hm_lpvt_2c2ab0a70d6928a332084e64a1f3117a=1449141674')
            ->timeoutIn(15)
            ->body($request)
            ->sendsForm()->send();
        return json_decode(mb_convert_encoding($response->body,"UTF8","GBK"));
    }

    public static function get_clxh($url,$vin_code){
        $request = array(
            'returnType' => 'div',
            'requestType' => 'vinsrNew',
            'vinCode' => $vin_code
        );

        $response = Httpful::post($url)
//            ->addHeader('Accept','application/json, text/javascript, */*; q=0.01')
//            ->addHeader('Accept-Encoding','gzip, deflate')
//            ->addHeader('Accept-Language','zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4')
//            ->addHeader('Connection','keep-alive')
//            ->addHeader('Content-Length','0')
            ->addHeader('Cookie','JSESSIONID=fvwdWflDSp07rlvLLd85PGJXLvGbzDMDBCRM8VClrZQ6vK0sCGZY!1315059231; Hm_lvt_2c2ab0a70d6928a332084e64a1f3117a=1449108707; Hm_lpvt_2c2ab0a70d6928a332084e64a1f3117a=1449141674')
//            ->addHeader('Host','pj.am123.com')
//            ->addHeader('Origin','http://pj.am123.com')
//            ->addHeader('Referer','http://pj.am123.com/jydataweb/enterFitSearch.do?initial=initial')
//            ->addHeader('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36')
//            ->addHeader('X-Requested-With','XMLHttpRequest')
            ->timeoutIn(15)
            ->body($request)
            ->sendsForm()->send();
        return json_decode(mb_convert_encoding($response->body,"UTF8","GBK"));
    }
}